;(function(window, document) {
    var share = {
        init: function () {
            var that = this;
            // 获取url地址
            var url = this.utils.getUrlParam('s');
            // 加载页面
            that.load(url);
            // 绑定事件
            that.bindEvent();
        },
        bindEvent: function () {
            var that = this;

            that.dom.close.addEventListener('click', eventClose, false);
            that.dom.btn.addEventListener('click', eventDownload, false);

            function eventClose (e) {
                e.preventDefault();
                that.dom.bar.parentNode.removeChild(that.dom.bar);
            }

            function eventDownload(e) {
                e.preventDefault();
                var os = that.parser.getOS().name.toLowerCase();
                console.log(os);
                if (os === 'ios') {
                    that.modalInit('', '正在跳转到应用页...', function () {
                        location.href = 'https://itunes.apple.com/us/app/zhang-hao-ban-lu/id992488773?l=zh&ls=1&mt=8';
                    });
                    return;
                }
                if (os === 'android') {
                    that.modalInit('', '正在跳转到应用页...', function () {
                        location.href = 'https://www.pgyer.com/CTcd';
                    });
                    return;
                }
                // 其他情况
                that.modalInit('请使用微信');
            }
        },
        load: function (url) {
            var that = this;
            if (/\?/.test(url)) {
                url += '&_t=' + Date.now();
            } else {
                url += '?_t=' + Date.now();
            }
            that.dom.iframe.src = url;
        },
        modalInit: function (hd, bd, callback) {
            var that = this;
            if (!hd || typeof hd != 'string') {
                hd = '';
            }
            if (!bd || typeof bd != 'string') {
                bd = '';
            }
            var timeStamp = Date.now();
            var modalId = 'modal_' + timeStamp;
            var closeId = 'close_' + timeStamp;
            var modal = that.utils.parseHTML([
                '<div class="m-modal" id="',
                modalId,
                '">',
                '<div class="inner">',
                hd === '' ?
                '<div class="hd" style="display: none;">' :
                '<div class="hd">',
                '<a href="javascript: void(0);" class="close" id="',
                closeId,
                '">x</a>',
                '<div class="caption">',
                hd,
                '</div>',
                '</div>',
                '<div class="bd">',
                bd,
                '</div>',
                '</div>',
                '</div>',
                '</div>'
            ].join(''))[0];
            that.utils.appendNode(that.dom.bar, modal);
            modal.style.display = 'flex';
            modal.style.justifyContent = 'center';
            modal.style.alignItems = 'center';

            if (hd !== '') {
                document.getElementById(closeId).addEventListener('click', eventCloseModal, false);
            }

            if (callback && typeof callback == 'function') {
                setTimeout(function() {
                    that.modalDestory(modalId);
                    callback();
                }, 2000);
            }
            function eventCloseModal (e) {
                e.preventDefault();
                that.modalDestory(modalId);
            }
        },
        modalDestory: function(modalId) {
            var modal = document.getElementById(modalId);
            modal.parentNode.removeChild(modal);
        },
        dom: {
            iframe: document.querySelector('.m-share-iframe'),
            bar: document.querySelector('.m-share-bar'),
            close: document.querySelector('.m-share-bar').querySelector('.close'),
            btn: document.querySelector('.m-share-bar').querySelector('.btn')
        },
        utils: {
            /**
             * 获取地址栏url中的参数值
             * @param {String} name - 参数名
             * @returns {String|null} 参数值
             */
            getUrlParam: function (name) {
                var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");//构造一个含有目标参数的正则表达式对象
                var r = window.location.search.substr(1).match(reg);  //匹配目标参数
                if (r !== null) return decodeURIComponent(r[2]);
                return null; //返回参数值
            },
            /**
             * 字符串转dom节点
             * @param  {String} str dom节点字符串
             * @return {Object} dom节点对象
             */
            parseHTML: function (str) {
                var div = document.createElement('div');
                div.innerHTML = str;
                return div.childNodes;
            },
            /**
             * 当前元素后添加元素
             * @param  {Object} targetNode 目标元素
             * @param  {Object} node       将要添加的元素
             * @return null
             */
            appendNode: function (targetNode, node) {
                targetNode.parentNode.insertBefore(node, targetNode.nextSibling);
            }
        },
        parser: new UAParser()
    };
    share.init();
})(window, document);
